package cn.bone.mima.base64;

import java.util.Base64;

public class TestBase64 {

    public static void main(String[] args) {
        String a1 = "1";
        String a2 = "12";
        String a3 = "123";
        String a4 = "武汉";
        String a5 = "☺☹☀";
        // 😀
        String a6 = "\uD83D\uDE00";

        Base64.Encoder encoder = Base64.getEncoder();
        byte[] e1 = encoder.encode(a1.getBytes());
        byte[] e2 = encoder.encode(a2.getBytes());
        byte[] e3 = encoder.encode(a3.getBytes());
        byte[] e4 = encoder.encode(a4.getBytes());
        byte[] e5 = encoder.encode(a5.getBytes());
        byte[] e6 = encoder.encode(a6.getBytes());

        /*
            如果原文的字节数正好是3的倍数，就不需要补齐
            如果原文的字节数/3 = 2，就需要补齐2个等号
            如果原文的字节数/3 = 1，就需要补齐1个等号

            // UTF-8中，一个中文是3个字节
            // UTF-8中，emoji表情是4个字节(好像☺☹☀这3个符号也都是3个字节的)
            // 😀 这个是4个字节的
         */

        // MQ==
        System.out.println(new String(e1));
        // MTI=
        System.out.println(new String(e2));
        // MTIz
        System.out.println(new String(e3));
        // 5q2m5rGJ
        System.out.println(new String(e4));
        // 4pi64pi54piA
        System.out.println(new String(e5));
        // 8J+YgA==
        System.out.println(new String(e6));
    }
}
